import pygame
from random import *

import main

#鱼类
class Fish(pygame.sprite.Sprite):
    def __init__(self,bg_size,fish_image,size,speed,num):  
        pygame.sprite.Sprite.__init__(self)
        
        #生成默认左朝向的鱼
        self.image = pygame.image.load(fish_image).convert_alpha()
        #将非透明部分标记为mask
        self.mask = pygame.mask.from_surface(self.image)
        
        #保存左右朝向的鱼图像
        self.Limage = self.image
        self.Rimage = pygame.transform.flip(self.Limage,True,False)
        #获取鱼矩形区域
        self.rect = self.image.get_rect()
        
        #鱼的大小级
        self.size = size
        #鱼的生命
        self.live = True
        #鱼游动速度
        self.speed = speed
        
        #限制小鱼活动范围
        self.width, self.height = bg_size[0], bg_size[1]

        #小鱼当前朝向(1:左  2:右)
        self.direction = num

        #初始化小鱼位置
        self.rect.center = (randint(800, 5 * self.width),randint(0, self.height-self.rect.height)) \
                           if self.direction==1 \
                           else (randint(-5 * self.width, 0),randint(0, self.height-self.rect.height))

    def reset(self):
        #如果朝左，重置位置并设为朝右
        if self.direction == 1:
            self.rect.center = \
                             (randint(800, 5 * self.width),\
                              randint(0, self.height-self.rect.height))
            self.direction == 2
        #如果朝左，重置位置并设为朝右
        elif self.direction == 2:
            self.rect.center = \
                             (randint(-5 * self.width, 0),\
                              randint(0, self.height-self.rect.height))
            self.direction == 1
            
    def swim(self):
        #向左游
        if self.direction == 1 and self.rect.right >= 0:
            self.rect.left -= self.speed
        elif self.direction ==1 and self.rect.right <= 0:
            self.reset()
        #向右游
        if self.direction == 2 and self.rect.left <= self.width:
            self.rect.left += self.speed
        elif self.direction ==2 and self.rect.left >= self.width:
            self.reset()

    def display_fish(self):
        #朝左图像
        if self.direction == 1:
            self.image = self.Limage
        #朝右图像
        elif self.direction == 2: 
            self.image = self.Rimage
        main.screen.blit(self.image,self.rect)

#小鱼类
class smallFish(Fish):
    pass

#中鱼类
class midFish(Fish):
    pass

#大鱼类
class bigFish(Fish):
    pass

#鲨鱼类
class Shark(pygame.sprite.Sprite):
    def __init__(self,bg_size,fish_image,num):  
        pygame.sprite.Sprite.__init__(self)
        
        #生成默认左朝向的鱼
        self.image = pygame.image.load(fish_image).convert_alpha()
        #将非透明部分标记为mask
        self.mask = pygame.mask.from_surface(self.image)
        
        #保存左右朝向的鱼图像
        self.Limage = self.image
        self.Rimage = pygame.transform.flip(self.Limage,True,False)
        #获取鱼矩形区域
        self.rect = self.image.get_rect()
        
        #鱼的大小级
        self.size = 6
        #鱼游动速度
        self.speed = 5
        
        #限制鱼活动范围
        self.width, self.height = bg_size[0], bg_size[1]

        #鱼当前朝向(1:左  2:右)
        self.direction = num

        #初始化鱼位置
        self.rect.center = ( 15 * self.width,randint(0, self.height-self.rect.height)) \
                            if self.direction==1 \
                            else (-15 * self.width,randint(0, self.height-self.rect.height))
            
    def reset(self):
            #如果朝左，重置位置并设为朝右
            if self.direction == 1:
                self.rect.center = (15 * self.width, randint(0, self.height-self.rect.height))
                self.direction == 2
            #如果朝左，重置位置并设为朝右
            elif self.direction == 2:
                self.rect.center = (-15 * self.width, randint(0, self.height-self.rect.height))
                self.direction == 1
            

    def swim(self):
        #向左游
        if self.direction == 1 and self.rect.right >= 0:
            self.rect.left -= self.speed
        elif self.direction ==1 and self.rect.right < 0:
            self.reset()
        #向右游
        if self.direction == 2 and self.rect.left <= self.width:
            self.rect.left += self.speed
        elif self.direction ==2 and self.rect.left > self.width:
            self.reset()

    def display_fish(self):
        #朝左图像
        if self.direction == 1:
            self.image = self.Limage
        #朝右图像
        elif self.direction == 2: 
            self.image = self.Rimage
        main.screen.blit(self.image,self.rect)
        
        
        
        
        
